package org.chen.pipeline;

import co.elastic.clients.elasticsearch.core.IndexResponse;
import com.alibaba.nacos.common.utils.StringUtils;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.chen.exception.StorageException;
import org.chen.model.Article;
import org.chen.model.ImageInfo;
import org.chen.model.Section;
import org.chen.model.elastic.ArticleIndex;
import org.chen.model.mongodb.ArticleDocument;
import org.chen.service.CrawlerService;
import org.chen.service.impl.CrawlerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public abstract class AbstractESPNPipeline implements Pipeline {
    @Autowired
    protected CrawlerServiceImpl crawlerService;

    @Setter
    private Boolean autoPublish = false;  // 默认不自动发布

    @Setter
    private Boolean crawlImages;

    @Override
    public void process(ResultItems resultItems, Task task) {
        String title = resultItems.get("title");
        if (title == null) {
            return;
        }

        try {
            // 创建文章对象
            Article article = buildArticle(resultItems);

            // 根据crawlImages标志决定是否处理图片
            if (crawlImages) {
                article.setImages(resultItems.get("images"));
            }

            // 使用注入的crawlerService保存文章
            crawlerService.storeArticle(article);

            // 记录日志
            logArticleInfo(article);

        } catch (Exception e) {
            log.error("Error processing article: {}", title, e);
        }
    }

    protected Article buildArticle(ResultItems resultItems) {
        Article article = new Article();
        article.setTitle(resultItems.get("title"));
        article.setAuthor(resultItems.get("author"));
        article.setTimestamp(resultItems.get("timestamp"));
        article.setLeadParagraphs(resultItems.get("leadParagraphs"));
        article.setSections(resultItems.get("sections"));
        article.setCategory(getCategory());
        article.setUrl(resultItems.get("url"));
        article.setImages(resultItems.get("images"));

        // 设置发布状态
        article.setStatus(Boolean.TRUE.equals(autoPublish) ? 1 : 0);
        log.info("Building article with status: {}, autoPublish setting: {}",
                article.getStatus(), autoPublish);

        return article;
    }

    // 子类需要实现的方法，返回具体的分类
    protected abstract String getCategory();

    // 日志记录方法
    private void logArticleInfo(Article article) {
        log.info("==== Article Found ====");
        log.info("URL: {}", article.getUrl());
        log.info("Title: {}", article.getTitle());
        log.info("Time: {}", article.getTimestamp());
        log.info("Author: {}", article.getAuthor());
        log.info("Status: {}", autoPublish ? "Published" : "Draft");
        log.info("Images: {}", crawlImages ? "Enabled" : "Disabled");
        // ... 其他日志信息 ...
        log.info("====================");
    }
}